<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: commentmodel.inc.php 5631 2011-01-14 08:48:44Z xuhui $
 */

if(!defined('IN_ADMIN')) {
	exit('Acess Denied');
}

require_once(B_ROOT.'./source/adminfunc/tool.func.php');

$commentmodelstr = '';
$commentmodel = $commentmodelarr = array();
$checkresults = array();

if(!empty($_POST['deletesubmit'])) {
	if ($_POST['operation'] == "delete") {
		if(!empty($_POST['cmids'])) {
			$cmid = implode(',', $_POST['cmids']);
			DB::query("DELETE FROM ".DB::table('commentmodels')." WHERE cmid IN ($cmid)");
			cpmsg('message_success', 'admin.php?action=commentmodel');
		} else {
			cpmsg('no_item', 'admin.php?action=commentmodel');
		}
	} else {
		cpmsg('no_operation', 'admin.php?action=commentmodel');
	}
}

if(($_GET['op'] == 'edit' || $_GET['op'] == 'add') && !empty($_POST['valuesubmit'])) {
	$scorename = '';
	$scorearr = array();
	$_POST['modelname'] = trim($_POST['modelname']);
	if(empty($_POST['modelname']) || bstrlen($_POST['modelname']) > 10) {
		array_push($checkresults, array('modelname'=>lang('commentmodel_modelname_length_error')));
	}
	$notfillednum = $fillednum = 0;
	for($i = 1; $i <= 8; $i++) {
		$_POST['score'.$i] = trim($_POST['score'.$i]);
		if(!empty($_POST['score'.$i])) {
			if(bstrlen($_POST['score'.$i]) > 10) {
				array_push($checkresults, array('score'.$i=>lang('commentmodel_modelname_length_error')));
			}
			$scorearr[$i] = $_POST['score'.$i];
			$fillednum++;
		} else {
			$notfillednum++;
		}
	}
	if($notfillednum == 8) {
		cpmsg('commentmodel_score_notwrite');
	}
	if(!empty($checkresults)) {
		cpmsg('add_error', '', 'error', '', true, true, $checkresults);
	}
	$scorename = serialize($scorearr);
	
	$categorylist = '';
	if(!empty($_POST['categorylist'])) {
	    
	    $categorylist = implode(",", $_POST['categorylist']);
	}

	DB::query("REPLACE INTO ".DB::table('commentmodels')." (cmid, modelname, scorenum, scorename, dateline, categorylist) VALUES ('$_POST[cmid]', '$_POST[modelname]', '$fillednum', '$scorename', '$_G[timestamp]', '$categorylist');");
    if(!empty($_POST['categorylist'])) {
        if(empty($_POST['cmid']))
            $_POST['cmid'] = DB::insert_id();
        $cats = array();
        foreach($_POST['categorylist'] as $catid) {
            $cats = array_merge($cats, getallsearchcats($catid,$_POST['cmid']));
        }
		DB::query('UPDATE '.DB::table('categories').' SET commtmodel=\'1\', cmid= \''.$_POST['cmid'].'\'  WHERE catid in (\''.implode("', '", $cats).'\') AND type=\'shop\' ');
    }
	cpmsg('message_success', 'admin.php?action=commentmodel');

} elseif($_GET['op'] == 'add' || $_GET['op'] == 'edit') {

	if($_GET['op'] == 'edit') {
		$commentmodelarr = DB::fetch(DB::query("SELECT * FROM ".DB::table('commentmodels')." WHERE cmid='$_GET[cmid]'"));
		$commentmodelarr['scorename'] = unserialize($commentmodelarr['scorename']);
		foreach($commentmodelarr['scorename'] as $key => $commentmodel) {
			$commentmodelarr['score'.$key] = $commentmodel;
		}
	}
	shownav('global', 'commentmodel_'.$_GET['op']);
	showsubmenu('commentmodel_add', array(
		array('nav_commentmodel', 'commentmodel', '0'),
		array('commentmodel_add', 'commentmodel&op=add', '1')
	));
	showtips('commentmodel_'.$_GET['op'].'_tips');
	showformheader('commentmodel&op='.$_GET['op']);
	showtableheader('');
    showsetting('commentmodel_name', 'modelname', $commentmodelarr['modelname'], 'text');
    $cateoptions = '';
	$categorylist = getcategory('shop');
	foreach($categorylist as $catid=>$categoryinfo) {
	    if($categoryinfo['upid'] == 0) {
	        $cateoptions .= '<option'.(in_array($catid, explode(",", $commentmodelarr['categorylist']))?' selected="selected"':'').' value="'.$catid.'">'.$categoryinfo['name'].'</option>';
	    }
	}
	echo '<tr><td class="td27" colspan="2">'.lang("commentmodel_categorylist").'</td></tr>';
    echo '<tr id="selectcategory" class="noborder"><td class="vtop rowform"><select style="width: 380px; height: 160px;" id="selcategorylist" name="categorylist[]" multiple="true">'.$cateoptions.'</select></td><td id="span_categorylist" class="vtop tips2">'.lang("commentmodel_categorylist_comment").'</td></tr>';
    /*echo '<tr style="display:none;"><td>
        <script type="text/javascript">
            $(function(){
                var upselect;
                $("#selectcategory").find("option").mousedown(function(){
                    upselect = $("#selcategorylist").val();
                });
                $("#selectcategory").find("option").mouseup(function(){
                    $("#selcategorylist").val(upselect);
                });
            });
            function autoselect(values) {
                //values = values.toString();
                $("#selcategorylist").val(values);
                var varr = values.split(",");
                for(var i=0; i<varr.length; i++) {
                    $("option[value="+varr[i]+"]").select();
                }
            }
        </script>
    </script></td><td></td></tr>';*/

	for($i = 1; $i <= 8; $i++) {
		showsetting('commentmodel_score'.$i, 'score'.$i, $commentmodelarr['score'.$i], 'text');
	}
	showhiddenfields(array('cmid' => $_GET['cmid']));
	showsubmit('valuesubmit');
	showtablefooter();
	showformfooter();
	bind_ajax_form();

} else {

	shownav('global', 'commentmodel_list');
	showsubmenu('nav_commentmodel', array(
		array('nav_commentmodel', 'commentmodel', '1'),
		array('commentmodel_add', 'commentmodel&op=add', '0')
	));
	showtips('commentmodel_list_tips');
	showformheader('commentmodel');
	showtableheader('');
	showsubtitle(array('<input type="checkbox" onclick="checkall(this.form, \'cmids\')" name="chkall" >', 'cmid', 'modelname', 'scorename', 'cmdateline', 'commentmodel_operation'));

	$query = DB::query('SELECT * FROM '.DB::table('commentmodels').' ORDER BY cmid ASC');
	while($commentmodel = DB::fetch($query)) {
		foreach(unserialize($commentmodel['scorename']) as $scorename) {
			$commentmodel['scorenamestr'] .= '['.$scorename.']';
		}
		$commentmodelarr[] = $commentmodel;
	}
	foreach($commentmodelarr as $value) {
		showtablerow('', array(), array("<input class='checkbox' type='checkbox' name='cmids[]' value='$value[cmid]' />", $value['cmid'], $value['modelname'], $value['scorenamestr'], date('Y-m-d', $value['dateline']), '<a href="admin.php?action=commentmodel&op=edit&cmid='.$value['cmid'].'">'.lang('commentmodeledit').'</a>'));
	}
	showcommentmod();
	showtablefooter();
	showformfooter();
	bind_ajax_form();
}
function getallsearchcats($catid, $cmid) {
    $catids[] = $catid;
    include_once(B_ROOT.'./batch.attribute.php');
    $cats = getsearchcats(getcategory('shop'), $catid);
    foreach($cats as $categoryinfo) {
        $catids[] = $categoryinfo['catid'];
    }
    return $catids;    
}
?>